﻿(function (exports) {

    var $ = null;

    var slbIpCnt = 0;

    var ipList = ['120.55.200.167'];

    var MAX_LOGIN_RETRY_CNT = 4;

    var loadJSFile = (function () {
        if (!cc.sys.isNative) {
            return function () {
            };
        }
        var path = jsb.fileUtils.getWritablePath() + (cc.sys.os == cc.sys.OS_IOS ? "/storage/" : "/");
        return function (file) {
            window.loaddedJscMap = window.loaddedJscMap || {};
            if (window.loaddedJscMap[file])
                return;
            if (jsb.fileUtils.isFileExist(path + file + 'c')) {
                if (cc.sys.os == cc.sys.OS_ANDROID) cc.loader.loadJs(path, file);
                else cc.loader.loadJs(path, file);

                if (cc.sys.os == cc.sys.OS_ANDROID) cc.loader.loadJs(path, file);
                else cc.loader.loadJs(path, file + 'c');

                window.loaddedJscMap[file] = 1;
            }
        }
    })();

    var LoginLayer = cc.Layer.extend({
        _mainScene: null,
        _ip: null,
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);

            window.nativeVersion = getNativeVersion();
        },
        onCCSLoadFinish: function () {
            var that = this;

            network.clearMsgs();
            network.start();

            window.onEnterCnt = window.onEnterCnt || 0;
            if (window.onEnterCnt == 0 && cc.sys.isNative) {
                window.onEnterCnt++;
                var path = jsb.fileUtils.getWritablePath() + (cc.sys.os == cc.sys.OS_IOS ? "/storage/" : "/");
                if (jsb.fileUtils.isFileExist(path + 'src/_files_.js')) {
                    if (cc.sys.os == cc.sys.OS_ANDROID) cc.loader.loadJs(path, 'src/_files_.js');
                    else cc.loader.loadJs('src/_files_.js');
                }

                if (jsb.fileUtils.isFileExist(path + 'src/_files_.jsc')) {
                    if (cc.sys.os == cc.sys.OS_ANDROID) cc.loader.loadJs(path, 'src/_files_.jsc');
                    else cc.loader.loadJs('src/_files_.jsc');
                }

                for (var i = 0; i < jsFiles.length; i++) {
                    if (jsFiles[i] == 'src/gameData.js')
                        continue;

                    var file = path + jsFiles[i];
                    if (jsb.fileUtils.isFileExist(file)) {
                        if (cc.sys.os == cc.sys.OS_ANDROID)
                            cc.loader.loadJs(path, jsFiles[i], function () {
                            });
                        else
                            cc.loader.loadJs(path, jsFiles[i], function () {
                            });
                    }

                    file += 'c';
                    if (jsb.fileUtils.isFileExist(file)) {
                        if (cc.sys.os == cc.sys.OS_ANDROID)
                            cc.loader.loadJs(path, jsFiles[i] + 'c', function () {
                            });
                        else
                            cc.loader.loadJs(path, jsFiles[i] + 'c', function () {
                            });
                    }
                }
            }

        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function () {
            this._super();

            var that = this;

            window.inReview = true;

            loadJSFile('src/gameData.js');
            loadJSFile('src/utils.js');

            cc.sys.localStorage.setItem('save_native_version', getNativeVersion());

            window.udid = fetchUDID();
            var getRes = cc.loader.getRes;
            cc.loader.getRes = function (url) {
                if (url && url.indexOf('.json') == url.length - 5 && !cc.loader.cache[url]) {
                    cc.loader.cache[url] = getRes.call(cc.loader, url);
                    return cc.loader.cache[url];
                }
                return getRes.call(cc.loader, url);
            };

            this._mainScene = new MainScene();
            this._mainScene.retain();

            loadCCSTo(res.LoginLayer_json, this, "Layer");
            $ = create$(this.getChildByName("Layer"));
            shipei($);

            $('root.barbg').setVisible(false);

            cc.Texture2D.defaultPixelFormat = cc.Texture2D.PIXEL_FORMAT_AUTO;

            gameData.voiceFlag = true;
            playMusic();

            TouchUtils.setOnclickListener($('root.login'), function (node) {
                cc.director.runScene(that._mainScene);
            });

            TouchUtils.setOnclickListener($('top.reset'), function (node) {
                cc.director.pushScene(new FightScene());
            });


            var spine = new sp.SkeletonAnimation('res/image/login/denglu.json', 'res/image/login/denglu.atlas');
            if(isIpad()){
                spine.setPosition(cc.p(0, 300 - 77));
            }else{
                spine.setPosition(cc.p(0, 300));
            }
            $('root.spine').addChild(spine);
            spine.addAnimation(0, 'animation', true);
        }
    });

    exports.LoginLayer = LoginLayer;

})(window);
